﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Service1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
            LoadData();
        Button3.Attributes.Add("onclick", "return confirm('Do you want to delete ?');");
    }

    private void LoadData()
    {
        ManagementCustomer m = new ManagementCustomer();
        GridView1.DataSource = m.GetCustomer();
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadData();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label10.Visible = true;
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

        ManagementCustomer m = new ManagementCustomer();
        if (m.DeleteCustomer(id))
        {
            GridView1.EditIndex = -1;
            LoadData();
            Label10.Visible = true;
            Label10.Text = "<span style='width: 200px;color:green;font-weight:bold;float: left;margin-top: 15px;'>Delete SuccerFully</span>";
        }
        else {
            Label10.Visible = true;
            Label10.Text = "<span style='width: 200px;color:red;font-weight:bold;float: left;margin-top: 15px;'>Delete Failed. Check Fkey</span>";
        
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        //((ImageButton)GridView1.FooterRow.Cells[0].FindControl("imgbDelAll")).Attributes.Add("onClick", "return confirm('Do you want to delete all?');");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowIndex < 0)
            return;
        if (GridView1.EditIndex != e.Row.RowIndex)
            ((ImageButton)e.Row.Cells[6].FindControl("imgbDel")).Attributes.Add("onClick", "return confirm('Do you want to delete ?');");
    }
    protected void imgbDelAll_Click(object sender, ImageClickEventArgs e)
    {
        ManagementCustomer m = new ManagementCustomer();
        bool flag = false;
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox temp = (CheckBox)item.Cells[0].FindControl("ckDel");
            if (temp.Checked)
            {
                string id = GridView1.DataKeys[item.RowIndex].Value.ToString();
                m.DeleteCustomer(id);
                flag = true;
            }
        }
        if (flag)
            LoadData();
    }
    protected void ckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ck = (CheckBox)GridView1.HeaderRow.Cells[0].FindControl("ckAll");
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox temp = (CheckBox)item.Cells[0].FindControl("ckDel");
            temp.Checked = ck.Checked;
        }
    }
    protected void imgbDel_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void ckDel_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ManagementCustomer m = new ManagementCustomer();
        GridView1.DataSource = m.SearchCustomer(txtSearch.Text);
        GridView1.DataBind();
    }
  
  
    protected void Button3_Click(object sender, EventArgs e)
    {
        ManagementCustomer m = new ManagementCustomer();
        bool flag = false;
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox temp = (CheckBox)item.Cells[0].FindControl("ckDel");
            if (temp.Checked)
            {
                string id = GridView1.DataKeys[item.RowIndex].Value.ToString();
                if (m.DeleteCustomer(id))
                { flag = true; }
            }
        }
        if (flag)
        {
            LoadData();
            Label10.Visible = true;
            Label10.Text = "<span style='width: 200px;color:green;font-weight:bold;float: left;margin-top: 15px;'>Delete SuccerFully</span>";
        }
        else
        {
            Label10.Visible = true;
            Label10.Text = "<span style='width: 200px;color:red;font-weight:bold;float: left;margin-top: 15px;'>Delete Failed. Check Fkey</span>";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex; 
        LoadData();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }
}